package base;

import static util.Constant.STR_INVALID_ID;
import static util.Constant.STR_ITEM_ALREADY_EXIST;
import static util.Constant.STR_ITEM_NOT_EXIST;

import java.util.HashSet;

/**
 * The User {@code class}. Manage the users and create User object
 * 
 * @author Peter NGO, Guillaume POUSSEL and Mathieu RUL
 * @version 1.0
 */
public class Itemlist {
	
	/**
	 * ID of the Itemlist.
	 */
	private Integer ID;
	
	/**
	 * Name of the Itemlist.
	 */
	private String name;
	
	/**
	 * List of items of the Itemlist.
	 */
	private HashSet<Integer> items;

	/**
	 * Constructor of the {@code class}. Create an Itemlist from parameter.
	 * 
	 * @param ID
	 *            the Itemlist ID.
	 * 
	 * @throws IllegalArgumentException
	 */
	public Itemlist(Integer ID) {
		if (ID < 0)
			throw new IllegalArgumentException(STR_INVALID_ID);

		this.ID = ID;
	}

	/**
	 * Constructor of the {@code class}. Create an Itemlist from parameter.
	 * 
	 * @param ID
	 *            the Itemlist ID.
	 * 
	 * @param name
	 *            the Itemlist name.
	 * 
	 * @param items
	 *            the items HashSet.
	 * 
	 * @throws IllegalArgumentException
	 */
	public Itemlist(Integer ID, String name, HashSet<Integer> items)
			throws IllegalArgumentException {
		if (ID < 0)
			throw new IllegalArgumentException(STR_INVALID_ID);

		this.ID = ID;
		this.name = name;
		this.items = items;
	}

	/**
	 * Add an Item to the Itemlist.
	 * 
	 * @param ID
	 *            the Item's ID.
	 * 
	 * @return {@code true} if the Item was added; {@code false} otherwise.
	 * 
	 * @throws Exception
	 */
	public boolean add(Integer ID) throws Exception {
		if (ID < 0)
			throw new IllegalArgumentException(STR_INVALID_ID);

		if (!items.add(ID))
			throw new Exception(STR_ITEM_ALREADY_EXIST);
		return items.add(ID);
	}

	/**
	 * Verify if this Itemlist contains an Item
	 * 
	 * @param ID
	 *            the Item's ID
	 *            
	 * @return {@code true} if the item is in the list; {@code false} otherwise
	 */
	public boolean contains(Integer ID) throws Exception {
        if (ID < 0)
            throw new IllegalArgumentException(STR_INVALID_ID);
        return items.contains(ID);
	}
	
	/**
	 * Check equality between two itemLists.
	 * 
	 * If this method get an Itemlist, it compares the two IDs, else it returns
	 * false.
	 * 
	 * @param object
	 *            Another object to compare.
	 * 
	 * @return {@code true} if the parameter are an Itemlist; {@code false}
	 *         otherwise.
	 */
	public boolean equals(Object object) {
		return ((object instanceof Itemlist) && ((Itemlist) object).getID() == this.ID);
	}
	
	/**
	 * Get the Itemlist's ID.
	 * 
	 * @return the ID.
	 */
	public Integer getID() {
		return ID;
	}

	/**
	 * Get the Itemlist's item list.
	 * 
	 * @return the itemLists.
	 */
	public HashSet<Integer> getItems() {
		return items;
	}

	/**
	 * Get the Itemlist's name.
	 * 
	 * @return the itemlist name.
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Get an hash representation of the Itemlist.
	 * 
	 * @return the hash representation of the Itemlist.
	 */
	@Override
	public int hashCode() {
		return name.hashCode();
	}

	/**
	 * Delete an Item from the Itemlist.
	 * 
	 * @param ID
	 *            the Item's ID.
	 * 
	 * @return {@code true} if the Item was removed; {@code false} otherwise.
	 * 
	 * @throws Exception
	 */
	public boolean remove(Integer ID) throws Exception {
		if (ID < 0)
			throw new IllegalArgumentException(STR_INVALID_ID);

		if (!items.remove(ID))
			throw new Exception(STR_ITEM_NOT_EXIST);

		return true;
	}

	/**
	 * Set the Itemlist's items list.
	 * 
	 * @param items
	 *            the items to set.
	 */
	public void setItems(HashSet<Integer> items) {
		this.items = items;
	}

	/**
	 * Set the Itemlist's name.
	 * 
	 * @param name
	 *            the name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Returns a {@code String} object representing the Itemlist's value.
	 * 
	 * @return a string representation of the Itemlist.
	 */
	@Override
	public String toString() {
		return "ItemList ID : " + ID + "\nName  : " + name + "\nItems "
				+ (items == null ? "" : items.toString());
	}
}
